#include <stdio.h>
   #include <pthread.h>

   #define NTHREADS 3
   void *CalculateSum(void *);
   pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
   int  counter = 0;

   #define N 10
   #define M 10

   int dimensional_array[N][M];

   main()
   {
      pthread_t thread_id[NTHREADS];
      int i, j;

      for (i = 0; i <= N - 1; i++ )
           for( j = 0; j <= M - 1; j++)
                   dimensional_array[i][j] = i;

      for(i=0; i < NTHREADS; i++)
      {
         pthread_create( &thread_id[i], NULL, CalculateSum, NULL );
      }

      for(j=0; j < NTHREADS; j++)
      {
         pthread_join( thread_id[j], NULL);
      }



      printf("Final counter value: %d\n", counter);

      //print ARRAY
      for (i = 0; i <= N-1; i++ ) {
           for( j = 0; j <= M-1; j++)
                   printf("%d\t",dimensional_array[i][j]);
           printf("\n");
           }
   }
   //Calculate
   void *CalculateSum(void *dummyPtr)
   {
      pthread_mutex_lock( &mutex1 );

    int i,j,sum = 0;

     for( i = 0; i <= N-1; i++) {
           for( j = 0; j <= M-1; j++) {
                   sum = dimensional_array[i][j] + dimensional_array[i][j];
           }
            printf(" Sum = %d\n", sum);
           }

      counter++;

      pthread_mutex_unlock( &mutex1 );
   }


